package com.qxl.forest.business.controller;

import com.qxl.forest.business.persistence.dao.OperationLogDao;
import com.qxl.forest.common.constants.RequestParams;
import com.qxl.forest.exception.BizExceptionEnum;
import com.qxl.forest.exception.BussinessException;
import com.qxl.forest.util.CommRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: qiuxinlin
 * @Dercription: 日志控制器
 * @Date: 10:56 2018/1/3
 */
@RestController
@RequestMapping("/log")
public class LogController extends BaseController{
    @Autowired
    OperationLogDao operationLogDao;

    /**
     * 获取所有日志
     * @param requestParams
     * @return
     */
    @PostMapping("/get")
    public CommRes getAll(@RequestBody RequestParams requestParams){
        if (requestParams.getPageSize() == 0) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        PageRequest pageRequest = new PageRequest(requestParams.getPageNumber(), requestParams.getPageSize());
        Page operationLogs=operationLogDao.findAll(pageRequest);
        return CommRes.success(operationLogs);
    }

    /**
     * 清空日志
     * @return
     */
    @DeleteMapping("/delete")
    public CommRes deleteAll(){
        operationLogDao.deleteAll();
        return CommRes.successRes();
    }
}
